﻿// https://www.anton.website/readonly-attribute/

using System;
using System.Collections;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace GameBasic.Editors
{
    [CustomPropertyDrawer(typeof(ShowOnlyAttribute))]
    public class ShowOnlyAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            //ShowOnlyAttribute att = (ShowOnlyAttribute)attribute;
            //object obj = property.serializedObject.targetObject;
            //Type type = obj.GetType();
            //FieldInfo field = type.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

            //string text = null;
            //if (field == null)
            //{
            //    text = $"Field is null: {property.type}";
            //}
            //else
            //{
            //    object val = field.GetValue(obj);

            //    if (val == null || val.ToString().Equals("null"))
            //        text = " <-This value should NOT be NULL!";
            //    else
            //        text = val.ToString();
            //}

            //EditorGUI.LabelField(position, string.Format("{0}: {1}", label.text, text));


            string text = "not supported";
            switch (property.propertyType)
            {
                case SerializedPropertyType.Generic:
                    //text = $"{property.objectReferenceValue}";
                    break;
                case SerializedPropertyType.Integer:
                    text = $"{property.intValue}";
                    break;
                case SerializedPropertyType.Boolean:
                    text = $"{property.boolValue}";
                    break;
                case SerializedPropertyType.Float:
                    text = $"{property.floatValue}";
                    break;
                case SerializedPropertyType.String:
                    text = $"{property.stringValue}";
                    break;
                case SerializedPropertyType.Color:
                    text = $"{property.colorValue}";
                    break;
                case SerializedPropertyType.ObjectReference:
                    text = $"{property.objectReferenceValue}";
                    break;
                case SerializedPropertyType.LayerMask:
                    text = $"{property.intValue}";
                    break;
                case SerializedPropertyType.Enum:
                    text = $"{property.enumValueIndex}";
                    break;
                case SerializedPropertyType.Vector2:
                    text = $"{property.vector2Value}";
                    break;
                case SerializedPropertyType.Vector3:
                    text = $"{property.vector3Value}";
                    break;
                case SerializedPropertyType.Vector4:
                    text = $"{property.vector4Value}";
                    break;
                case SerializedPropertyType.Rect:
                    text = $"{property.rectValue}";
                    break;
                case SerializedPropertyType.ArraySize:
                    text = $"{property.arraySize}";
                    break;
                case SerializedPropertyType.Character:
                    text = $"{property.stringValue}";
                    break;
                case SerializedPropertyType.AnimationCurve:
                    text = $"{property.animationCurveValue}";
                    break;
                case SerializedPropertyType.Bounds:
                    text = $"{property.boundsValue}";
                    break;
                case SerializedPropertyType.Gradient:
                    break;
                case SerializedPropertyType.Quaternion:
                    text = $"{property.quaternionValue}";
                    break;
                case SerializedPropertyType.ExposedReference:
                    text = $"{property.exposedReferenceValue}";
                    break;
                case SerializedPropertyType.FixedBufferSize:
                    text = $"{property.fixedBufferSize}";
                    break;
                case SerializedPropertyType.Vector2Int:
                    text = $"{property.vector2IntValue}";
                    break;
                case SerializedPropertyType.Vector3Int:
                    text = $"{property.vector3IntValue}";
                    break;
                case SerializedPropertyType.RectInt:
                    text = $"{property.rectIntValue}";
                    break;
                case SerializedPropertyType.BoundsInt:
                    text = $"{property.boundsIntValue}";
                    break;
                case SerializedPropertyType.ManagedReference:
                    text = $"{property.managedReferenceValue}";
                    break;
                case SerializedPropertyType.Hash128:
                    text = $"{property.hash128Value}";
                    break;
                default:
                    break;
            }

            EditorGUI.LabelField(position, string.Format("{0}: {1}", label.text, text));
        }
    }
}